package com.chenss.dragonserapp.app;

import android.app.Application;

import com.kejiang.yuandl.mylibrary.CustomActivityOnCrash;
import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.https.HttpsUtils;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.OkHttpClient;

/**
 * PageName: application
 * Create by chenss at 2018/4/20 0:24
 */
public class DserApplication extends Application {
    public static boolean isDebug = true ;
    public static String APP_NAME="DRAGONSER " ;
    //单利模式
    private static DserApplication instance = null;
    public static DserApplication getInstance(){
        if (instance == null){
            instance = new DserApplication();
        }
        return instance;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        initOkhttp();
        initLogger();
        customCrash();
    }

    /**
     * okhttp初始化
     */
    private void initOkhttp() {
        InputStream inputStream = null;
        try {
            inputStream = getAssets().open("server.crt");

        } catch (Exception e) {

        }
        // HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(new InputStream[]{inputStream}, null, null);
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);


        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                })
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);
    }


    /**
     * Logger初始化
     */
    private void initLogger() {
        //自定义输出TAG
        Logger.init("Dser")
                //测试阶段设置日志全部输出 上线关闭 （LogLevel.NONE）
                .logLevel(LogLevel.FULL);

    }
    /**
     * 自定义异常捕捉
     */
    private void customCrash() {
        CustomActivityOnCrash.install(this);
//        String[] emialTo = {"xxxxxxxxx@qq.com"};//配置邮箱
//        CustomActivityOnCrash.setEmailTo(emialTo);
        CustomActivityOnCrash.setDebugMode(true);
    }


}
